package com.itbaizhan.controller;

import com.itbaizhan.domain.Student;
import com.itbaizhan.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /*参数model 把数据传到jsp页面中*/
    @RequestMapping("/all")
    public String all(Model model){
        List<Student> students = studentService.findStudent();
        model.addAttribute("students",students);
        return "allstudent";
    }
    /*没有jsp页面时，用于测试，在dao层实例化的student，会通过service层传到这里
    * 如果有jsp页面时，student是通过jsp页面传进来*/
    @RequestMapping("/add")
    public String addStudent(Student student){
        studentService.addStudent(student);
        //重定向到查询学生
        return "redirect:/student/all";
    }

}
